package com.wallet.crypto.trustapp.ui.widget.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wallet.crypto.trustapp.R;
import com.wallet.crypto.trustapp.ui.widget.entity.VirtuaCurrencyItem;
import com.wallet.crypto.trustapp.ui.widget.holder.VirtuaCurrencyHolder;
import com.wallet.crypto.trustapp.util.IPreference;
import com.wallet.crypto.trustapp.util.PreferenceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lingjianzhong on 2018/5/8.
 */

public class VirtuaCurrencyAdapter extends RecyclerView.Adapter<VirtuaCurrencyHolder> {

    ArrayList<VirtuaCurrencyItem> mItems;
    Context mContext;
    LayoutInflater mInflater;
    private String spStr;
    private Map<String, Boolean> spList = new HashMap<>();
    // 存储勾选框状态的map集合
    private Map<Integer, Boolean> map = new HashMap<>();
    IPreference mIPreference;
    private Gson gson;

    public VirtuaCurrencyAdapter(Context context, ArrayList<VirtuaCurrencyItem> mItems) {
        this.mContext = context;
        this.mItems = mItems;
        this.mInflater = LayoutInflater.from(mContext);
        mIPreference = PreferenceImpl.getPreference(mContext);
        gson = new Gson();
        spStr = mIPreference.getString("Virtua");
        if (spStr != null)
            spList = gson.fromJson(spStr, new TypeToken<Map<String, Boolean>>() {
            }.getType());
        initMap();//初始化选择
    }

    private void initMap() {
        for (int i = 0; i < mItems.size(); i++) {
            if (spStr == null) {
                map.put(i, false);
            } else {
                if (spList.size() > 0) {
                    if (spList.containsKey(mContext.getString(mItems.get(i).currencyName).trim()))
                        map.put(i, true);
                    else
                        map.put(i, false);
                } else {
                    map.put(i, false);
                }
            }
        }
    }

    @NonNull
    @Override
    public VirtuaCurrencyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.item_togglebutton_rev, parent, false);
        return new VirtuaCurrencyHolder(itemView);

    }

    @Override
    public void onBindViewHolder(@NonNull VirtuaCurrencyHolder holder, int position) {
        holder.mName.setText(mItems.get(position).currencyName);
        holder.mToggleButton.setChecked(map.get(position));
        holder.mToggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                map.put(position, isChecked);
                if (isChecked) {
                    if (!spList.containsKey(holder.mName.getText().toString().trim()))
                        spList.put(holder.mName.getText().toString().trim(), isChecked);
                } else {
                    spList.remove(holder.mName.getText().toString().trim());
                }
                mIPreference.put("Virtua", gson.toJson(spList));
            }
        });
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }
}